import UI.Admin;
import UI.Loginerror;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
/*
 * Created by JFormDesigner on Thu May 25 20:02:42 CST 2023
 */



/**
 * @author wxz
 */
public class Login extends JFrame {
    Admin user = new Admin();
    public Login( ) {
        initComponents();
    }

    public Login( Admin  ad) {
        initComponents();
        this .user = ad;
    }

    private void ok(ActionEvent e) {
        // TODO add your code here
        String userAccount = textField1.getText();
        String userPassword = textField2.getText();
        //输入的账号和密码进行合法性检查


        //

        Admin admin = new Admin(userAccount , userPassword);

        if(admin.equal(user)){
            //关闭登陆界面， 进入主界面
            manger Manger = new manger();
            Manger.setVisible(true);
            Manger.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           this.dispose();
        }else {
            Loginerror  error = new Loginerror(this);
            //DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE
            error.setVisible(true);
            error.setDefaultCloseOperation(HIDE_ON_CLOSE);
        }

    }

    private void cancel(ActionEvent e) {
        // TODO add your code here

    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("Gui");
        dialogPane = new JPanel();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();
        desktopPane1 = new JDesktopPane();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();

        //======== this ========
        setTitle(bundle.getString("login.this.title"));
        setPreferredSize(new Dimension(480, 360));
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setPreferredSize(new Dimension(190, 60));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText(bundle.getString("login.okButton.text"));
                okButton.setPreferredSize(new Dimension(61, 43));
                okButton.setFont(new Font("\u534e\u6587\u884c\u6977", Font.PLAIN, 19));
                okButton.addActionListener(e -> ok(e));
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText(bundle.getString("login.cancelButton.text"));
                cancelButton.setPreferredSize(new Dimension(57, 30));
                cancelButton.setFont(new Font("\u534e\u6587\u884c\u6977", Font.PLAIN, 19));
                cancelButton.addActionListener(e -> cancel(e));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);

            //======== desktopPane1 ========
            {
                desktopPane1.setBackground(Color.white);

                //---- label1 ----
                label1.setText(bundle.getString("login.label1.text"));
                label1.setBackground(new Color(0xcccccc));
                label1.setFont(new Font("\u534e\u6587\u884c\u6977", Font.PLAIN, 22));
                desktopPane1.add(label1, JLayeredPane.DEFAULT_LAYER);
                label1.setBounds(35, 90, 70, 45);
                desktopPane1.add(textField1, JLayeredPane.DEFAULT_LAYER);
                textField1.setBounds(120, 95, 230, 35);

                //---- label2 ----
                label2.setText(bundle.getString("login.label2.text"));
                label2.setFont(new Font("\u534e\u6587\u884c\u6977", Font.PLAIN, 22));
                desktopPane1.add(label2, JLayeredPane.DEFAULT_LAYER);
                label2.setBounds(35, 130, 70, 50);
                desktopPane1.add(textField2, JLayeredPane.DEFAULT_LAYER);
                textField2.setBounds(120, 140, 230, 35);
            }
            dialogPane.add(desktopPane1, BorderLayout.CENTER);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel dialogPane;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private JDesktopPane desktopPane1;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
